package push.tools.service.api;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import push.tools.service.framework.PostController;
import push.tools.service.service.PushTaskService;
import push.tools.service.utils.TimeValidator;
import push.tools.service.utils.UUIDUtils;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/main", produces = {"application/json;charset=UTF-8"})
public class MAIN_000_000_001 extends PostController {

    private static final Logger logger = LoggerFactory.getLogger(MAIN_000_000_001.class);

    @Autowired
    public PushTaskService pushTaskService;

    //创建推送任务接口
    @PostMapping(value = "/000_000_001")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(HashMap<String, String> requestParam) {
        String requestBody = requestParam.get("requestBody");
        JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String pushId = UUIDUtils.generate();
        //校验时间
        boolean b = TimeValidator.validateTimeParameters(startTime, endTime);
        if (!b) {
            return generateFail("时间校验不通过，请检查时间格式");
        }
        pushTaskService.executeTask(pushId, startTime, endTime);
        //封装响应体
        HashMap<String, String> resMap = new HashMap<>();
        resMap.put("message", "任务已提交，后台正在处理...");
        return generateSucceed(resMap);
    }
}
